package rs.etf.kdp.server;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import rs.etf.kdp.client.Job;

public class Log {
	private static FileWriter logFile;
	private static BufferedWriter logWriter;
	
	static 
	{
		try {
			logFile = new FileWriter("res/server_log.log", true);
		} catch (IOException e) {
			System.out.println("Error while opening log file");
		}
		
		logWriter = new BufferedWriter(logFile);
	}
	
	public static void receivedJob(Job j)
	{
		long jobId = j.getServerID();
		String from = j.getIdentifier();
		
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        String dateString  = dateFormat.format(date);
        
        String intoLog = "Received job form " + from + " with identifier " + jobId + " date/time: " + dateString;
        try {
			logWriter.append(intoLog + "\n");
		} catch (IOException e) {
			System.out.println("Error while writing in file when job is received");
		}
		
		try {
			logWriter.flush();
		} catch (IOException e) {
			System.out.println("Error while flushing log");
		}
	}
	
	public static void changedState(long id, String newState)
	{
		String intoLog = "Changed state of job with ID " + id + " to " + newState;
		
		try {
			logWriter.append(intoLog + "\n");
		} catch (IOException e) {
			System.out.println("Error while writing in file when job state changed");
		}
		
		try {
			logWriter.flush();
		} catch (IOException e) {
			System.out.println("Error while flushing log");
		}
	}
	
	public static void partsSentTo(List<Node> willSimulateOn)
	{
		StringBuilder workstations = new StringBuilder();
		for(Node i : willSimulateOn)
		{
			workstations.append(i.getNameOfNode());
			workstations.append(":");
			workstations.append(i.getIpAddress());
			workstations.append(" ");
		}
		
		String wss = workstations.toString();
		String intoLog = "Job is transported to following workstations " + wss;
		try {
			logWriter.append(intoLog + "\n");
		} catch (IOException e) {
			System.out.println("Error while writing in file when sending parts");
		}
		
		try {
			logWriter.flush();
		} catch (IOException e) {
			System.out.println("Error while flushing log");
		}
	}
	
	public static void jobFinished(long jobId)
	{
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        String dateString  = dateFormat.format(date);
        
        String intoLog = "Job with id " + jobId + " is finished " + dateString;
        
        try {
			logWriter.append(intoLog + "\n");
		} catch (IOException e) {
			System.out.println("Error while writing in file when job is finished");
		}
		
		try {
			logWriter.flush();
		} catch (IOException e) {
			System.out.println("Error while flushing log");
		}
	}
	
	public static void closeLog()
	{
		try {
			logWriter.close();
		} catch (IOException e) {
			System.out.println("Error while closing log file");
		}
	}
}
